package com.hamizhong.iflowers.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.hamizhong.iflowers.db.dao.hibernate.IFestivalDao;
import com.hamizhong.iflowers.db.entity.Festival;
import com.hamizhong.iflowers.service.IFestivalService;
import com.hamizhong.iflowers.service.base.MyBaseService;
import com.hamizhong.iflowers.utils.MyErrorMsgConstants;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.FestivalVO;
import com.hamizhong.iflowers.web.vo.IObjectVO;

public class FestivalServiceImpl extends MyBaseService implements IFestivalService {
	private IFestivalDao iFestivalDao = null;

	public IFestivalDao getiFestivalDao() {
		return iFestivalDao;
	}
	public void setiFestivalDao(IFestivalDao iFestivalDao) {
		this.iFestivalDao = iFestivalDao;
	}
	
	public IOPResultDto<IObjectVO> findAll(Integer pageNum, int pageSize) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		List<Festival> festivalList = new ArrayList<Festival>();
		festivalList = iFestivalDao.findFestival(pageNum, pageSize);
		int totalFestivalCnt = iFestivalDao.findTotalFestivalCnt();
		List<FestivalVO> festivalVOList = new ArrayList<FestivalVO>();
		FestivalVO festivalVO = null;
		for (Festival festival : festivalList) {
			festivalVO = new FestivalVO();
			BeanUtils.copyProperties(festival, festivalVO);
			festivalVOList.add(festivalVO);
		}
		IObjectVO iObjectVO = new IObjectVO();
		iObjectVO.setFestivalVOList(festivalVOList);
		iObjectVO.setTotalFestivalCnt(totalFestivalCnt);
		dto.addToResultList(iObjectVO);
		dto.setSuccess(true);
		return dto;
	}
	public IOPResultDto<IObjectVO> addFestival(FestivalVO festivalVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		Festival festival = new Festival();
		try {
			BeanUtils.copyProperties(festivalVO, festival);
			iFestivalDao.add(festival);
			dto.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			dto.setSuccess(false);
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_ADD_OPERATE);
		}
		return dto;
	}
	public IOPResultDto<IObjectVO> updateFestival(FestivalVO festivalVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		Festival festival = new Festival();
		try {
			BeanUtils.copyProperties(festivalVO, festival);
			iFestivalDao.update(festival);
			dto.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			dto.setSuccess(false);
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_UPDATE_OPERATE);
		}
		return dto;
	}
	public IOPResultDto<IObjectVO> deleteFestival(FestivalVO festivalVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		try {
			String festivalId = festivalVO.getFestivalId();
			Festival festival = iFestivalDao.findById(festivalId);
			if(null != festival) {
				this.iFestivalDao.delete(festival);
				dto.setSuccess(true);
			}else {
				dto.setSuccess(false);
				dto.setErrorMSG(MyErrorMsgConstants.NOT_EXIST_DATA_FOR_OPERATE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			dto.setErrorMSG(MyErrorMsgConstants.ERR_SYSTEM);
			dto.setSuccess(false);
		}
		return dto;
	}
	
}
